package myLinKedList;

public class MyLinKedList implements ILIst{


    static class ListNode {
        int val;
        ListNode prve;
        ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if(this.head == null) {
            this.head = node;
            this.last = node;
        }else {
            node.next = this.head;
            this.head.prve = node;
            this.head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if(this.head == null) {
            this.head = node;
            this.last = node;
        }else {
            this.last.next = node;
            node.prve = this.last;
            this.last = node;
        }
    }

    @Override
    public void addIndex(int index, int data) {
        int len = size();
        if(index < 0 || index > len) {
            //抛出异常
            System.out.println("插入index 不合法");
            return ;
        }
        if(index == 0) {
            addFirst(data);
            return;
        }
        if(index == len) {
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        while(index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        cur.next.prve = node;
        node.next = cur.next;
        cur.next = node;
        node.prve = cur;
    }

    @Override
    public boolean contains(int key) {
       ListNode cur = this.head;
       while(cur != null) {
           if(cur.val == key) {
               return true;
           }
           cur = cur.next;
       }
       return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur == head) {
                    cur = head.next;
                    if(head.next == null) {
                        head = null;
                        last = null;
                    }else {
                        head = cur;
                        head.prve = null;
                    }
                }else if(cur == last) {
                    last = cur.prve;
                    last.next = null;
                } else {
                    cur.prve.next = cur.next;
                    cur.next.prve = cur.prve;
                }
                return ;
            }
            cur = cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.val == key) {
                if(cur == head) {
                    cur = head.next;
                    if(head.next == null) {
                        head = null;
                        last = null;
                    }else {
                        head = cur;
                        head.prve = null;
                        continue;
                    }
                }else if(cur == last) {
                    last = cur.prve;
                    last.next = null;
                } else {
                    cur.prve.next = cur.next;
                    cur.next.prve = cur.prve;
                }
            }
            cur = cur.next;
        }
    }

    @Override
    public int size() {
        int size = 0;
        ListNode cur = this.head;
        while(cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    @Override
    public void display() {
        ListNode cur = this.head;
        while(cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    @Override
    public void clear() {

    }
}
